AddCSLuaFile( "init.lua" )

local SolidWhite = CreateMaterial( "SolidWhite_z", "UnlitGeneric", {
    ["$basetexture"] = "color/white",
    ["$vertexcolor"] = "1",
    ["$vertexalpha"] = "1",
    ["$nolod"] = "1",
} )

local Glow = Material( "sprites/physg_glow1" )
function EFFECT:Init( data )

	self:SetModel( "models/weapons/w_missile_launch.mdl" )
	
	self.Entity.Die = CurTime() + 3
	self.Entity.Pos = data:GetStart()
	self.Entity.End = data:GetOrigin()
	self.Entity.TargetEntity = data:GetEntity()
	if ValidEntity( self.Entity.TargetEntity ) then
		self.Entity.End = self.Entity.TargetEntity:GetPos()
	end
	self.Entity.Normal = ( self.Entity.End - self.Entity.Pos ):GetNormal()
	self.Entity.Start = CurTime()
	self.Entity.Dist = self.Entity.Pos:Distance( self.Entity.End )
	self.Entity.CurPos = self.Entity.Pos
	
	self.Entity.MuzzleDie = CurTime() + 0.05
	self.Entity.NextSmoke = CurTime() + 0.2
	
end

function EFFECT:Think( )

	if CurTime() > self.Die or self.Entity.CurPos:Distance( self.Entity.Pos ) > self.Entity.Dist then
		local effectdata = EffectData()
		effectdata:SetStart( self.Entity.CurPos-self.Entity.Normal*10 ) // not sure if we need a start and origin (endpoint) for this effect, but whatever
		effectdata:SetOrigin( self.Entity.CurPos-self.Entity.Normal*10 )
		effectdata:SetScale( 0.5 )
		util.Effect( "cball_explode", effectdata )	
		return false
	end
	
	if CurTime() > self.Entity.NextSmoke then
		local emitter = ParticleEmitter( self.Entity.CurPos )
		local particle = emitter:Add( "particle/smokesprites_0001", self.Entity.CurPos )
		if (particle) then
			
			particle:SetVelocity( Vector(0,0,0) )
			
			particle:SetLifeTime( 0 )
			particle:SetDieTime( 2 )
			
			particle:SetColor(255,128,0)			

			particle:SetStartAlpha( 100 )
			particle:SetEndAlpha( 0 )
			
			particle:SetStartSize( 5 )
			particle:SetEndSize( 10 )
			
			particle:SetRoll( math.Rand(-360, 360) )
			particle:SetRollDelta( math.Rand(-0.21, 0.21) )
			
			particle:SetAirResistance( 250 )
			
			particle:SetGravity( Vector(0,0,10) )

			particle:SetCollide( false )
			particle:SetBounce( 0.42 )

			particle:SetLighting(true)
		
			
		end
		emitter:Finish()
		self.Entity.NextSmoke = CurTime() + 0.01
	end
	
	
	return true
end

function EFFECT:Render()

	if ValidEntity( self.Entity.TargetEntity ) then
		self.Entity.End = self.Entity.TargetEntity:LocalToWorld(self.Entity.TargetEntity:OBBCenter())
		self.Entity.Dist = self.Entity.Pos:Distance( self.Entity.End )
		self.Entity.Normal = ( self.Entity.End - self.Entity.Pos ):GetNormal()
	end
	
	local ent_pos = self.Entity.Pos + self.Entity.Normal * ( CurTime() - self.Entity.Start ) * 400
	
	render.SetMaterial( Glow )
	render.DrawSprite( ent_pos + self.Entity.Normal*-5, 15, 15, Color( 255, 255, 255, 15 ) )
	render.DrawSprite( ent_pos + self.Entity.Normal*-20, 50, 50, Color( 250, 139, 39, 15 ) )

	if CurTime() < self.Entity.MuzzleDie then
		local Scale = ( CurTime() - self.Entity.MuzzleDie )/0.01
		render.DrawSprite( self.Entity.Pos, 15+5*Scale, 15, Color( 250, 139, 39, 155 ) )
		render.DrawSprite( self.Entity.Pos, 8+5*Scale, 8, Color( 255, 255, 255, 255 ) )
	end

	render.SetMaterial( SolidWhite )
	render.StartBeam( 5 )
	render.AddBeam( ent_pos + self.Entity.Normal*(75-40), 0, 0, Color( 250, 139, 39, 255 ) )
	render.AddBeam( ent_pos + self.Entity.Normal*(70-40), 5, 0, Color( 255, 255, 255, 35 ) )
	render.AddBeam( ent_pos + self.Entity.Normal*(60-40), 6, 0, Color( 255, 255, 255, 35 ) )
	render.AddBeam( ent_pos + self.Entity.Normal*(40-40), 4, 0.6, Color( 250, 139, 39, 35 ) )
	render.AddBeam( ent_pos, 0.5, 1, Color( 250, 139, 39, 0 ) )
	render.EndBeam()
	
	render.StartBeam( 5 )
	render.AddBeam( ent_pos + self.Entity.Normal*(75-40), 0, 0, Color( 255, 255, 255, 255 ) )
	render.AddBeam( ent_pos + self.Entity.Normal*(70-40), 3, 0, Color( 255, 255, 255, 255 ) )
	render.AddBeam( ent_pos + self.Entity.Normal*(60-40), 3, 0, Color( 251, 201, 151, 255 ) )
	render.AddBeam( ent_pos + self.Entity.Normal*(40-40), 1, 0.6, Color( 250, 139, 39, 155 ) )
	render.AddBeam( ent_pos, 0.1, 1, Color( 250, 139, 39, 55 ) )
	render.EndBeam()
	
	self:SetPos( ent_pos + self.Entity.Normal )
	self:SetAngles( self.Entity.Normal:Angle() )
	self:DrawModel()
	
	self.Entity.CurPos = ent_pos
	
end
